package com.esteel.settlement.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.esteel.settlement.dao.SettTranRepository;
import com.esteel.settlement.entity.TbSettFund;
import com.esteel.settlement.entity.TbSettTran;
import com.esteel.settlement.service.SettTranService;

@Service
public class SettTranServiceImpl implements SettTranService{

	@Autowired
	private SettTranRepository settTranRepository;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public TbSettTran add(TbSettTran entity) {
		return settTranRepository.save(entity);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TbSettTran> selectTran(TbSettTran entity) {
		// TODO Auto-generated method stub
//		Example<TbSettTran> e= Example.of(entity);
//		return settTranRepository.findAll(e);
		SimpleDateFormat SFDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strb=SFDate.format(entity.getBeginDate());//entity.getTranDate();
		String stre=SFDate.format(entity.getEndDate());//entity.getTranDate();
		List<TbSettTran> tb= jdbcTemplate.query("select * from TB_SETT_TRANS t where t.tran_date>=to_date (? , 'YYYY-MM-DD HH24:MI:SS' ) and t.tran_date<=to_date (? , 'YYYY-MM-DD HH24:MI:SS' )", new Object[]{strb,stre},new RowMapper(){

			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				TbSettTran tb= new TbSettTran();
				tb.setThirdLogNo(rs.getString("THIRD_LOG_NO"));
				tb.setFrontLogNo(rs.getString("frontLogNo"));
				tb.setInCustAcctId(rs.getString("IN_CUST_ACCT_ID"));
				tb.setInThirdCustId(rs.getString("IN_THIRD_CUST_ID"));
				tb.setOutCustAcctId(rs.getString("OUT_CUST_ACCT_ID"));
				tb.setOutThirdCustId(rs.getString("OUT_THIRD_CUST_ID"));
				tb.setTranAmount(rs.getBigDecimal("tranAmount"));
				tb.setTranFlag(rs.getBigDecimal("TRAN_STATUS"));
				tb.setTranDate(rs.getDate("TRAN_DATE"));
				return tb;
			}	
			
		});
//		jdbcTemplate.queryForList("select * from tb_sett_fund t where t.sup_acct_id=?", new Object[]{str}, new TbSettFund());
		System.out.println(tb.size());
		return tb;//settFundRepository.findAll(e);
		
	}

}
